﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow.Domain.Enums;
using Workflow.Domain.Models;
using Workflow.Infrastructure.IRepository;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace Workflow.Application.Step
{
    public class EndStep : StepBodyAsync
    {
        private readonly IBaseRepository<WorkflowInstanceModel> instanceBase;

        public EndStep(IBaseRepository<WorkflowInstanceModel> instanceBase)
        {
            this.instanceBase = instanceBase;
        }

        public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
        {
            var instance = await instanceBase.GetValue(x => x.WorkflowInstanceId == context.Workflow.Id);
            instance.StatuEnum = WorkflowInstanceStatuEnum.Completed;
            instance.CurrentStepId = "";
            instance.CurrentStepName = "";
            instance.CurrentAuditUsers = "";
            instance.UpdatedTime = DateTime.Now;
            await instanceBase.UpdateRange(instance);
            context.Workflow.Status = WorkflowStatus.Complete;
            context.Workflow.CompleteTime = DateTime.Now;
            Console.WriteLine("流程结束！");
            return ExecutionResult.Next();
        }
    }
}
